﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Framework.DataObjects;
using Framework.Interface;

namespace Framework
{
    /// <summary>
    /// Name/Value data object, The value part serializes as an XML Text value
    /// </summary>
    [Serializable]
    public class NameTextValueDo : DomObject, INameValue
    {
        #region C'tor
        public NameTextValueDo()
        {
        }

        public NameTextValueDo(string name, string value)
        {
            Name = name;
            Value = value;
        }
        #endregion

        [XmlAttribute("Name")]
        public string Name { get; set; }

        [XmlText]
        public string Value { get; set; }

        public NameTextValueDo ShallowCopy()
        {
            return (NameTextValueDo)MemberwiseClone();
        }

        // *********************************************************************************
        // Static Helper
        // *********************************************************************************

        #region CreateFromStringArray
        /// <summary>
        /// Creates a list of NameTextValueDo objects from a string/value pair array.
        /// </summary>
        /// <param name="nameValuePairs">The name value pairs.</param>
        /// <returns></returns>
        public static List<NameTextValueDo> CreateFromStringArray(params string[] nameValuePairs)
        {
            if (Is.EmptyArray(nameValuePairs))
            {
                return new List<NameTextValueDo>();
            }

            if (nameValuePairs.Length % 2 != 0)
            {
                throw new Exception("");
            }

            var result = new List<NameTextValueDo>();

            for (var i = 0; i < nameValuePairs.Length; i+=2)
            {
                result.Add(new NameTextValueDo(nameValuePairs[i], nameValuePairs[i + 1]));
            }

            return result;
        }
        #endregion
    }
}
